home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / windc.exe / SAMPLE1.C < prev    next >
C/C++ Source or Header  |  1991-07-07  |  13KB  |  410 lines

  1. /* sample of basic functions */
  2. #include <stdio.h>
  3. #include <dos.h>
  4. #include <memory.h>
  5. #include "wwindefs.h"                /* window headers */
  6. #include "wwinstpr.h"
  7.  
  8. /* subroutine prototypes */
  9. void Example1();
  10. void Example2();
  11. void Example3();
  12. void Example4();
  13. void Example5();
  14.  
  15. extern WWVIDMEM WWscreenp;                 /* pointer to screen memory */
  16. extern unsigned int WWScreenLength;        /* access to WW defaults */
  17. extern unsigned int WWScreenWidth;
  18.  
  19. /* define caption window  - globally */
  20. struct WWinstruc Caption =
  21.   {"Caption",22,2,10,60, WHITE,BLACK,SLN_BDR,WHITE,BLACK,INV_CSR};
  22.  
  23. main()
  24.   {
  25.   int i, rc;
  26.   WWscreenp = WWCOLMEM;  /* unecessary - color screen memory is default*/
  27.   for (i=0;i<27;i++)               /* clear screen */
  28.     printf("                                          "
  29.        "                                          ");
  30.  
  31.   if ((rc = WWinDef(&Caption)) !=0)     /* set up caption screen */
  32.     {printf("out of memory"); exit(1);}
  33.   WWinLoad(&Caption,2,2,NC,NC,"Press ESC to exit this example",0);
  34.   WWinPut(&Caption);
  35.   WWinCurSet(&Caption,1,1);             /* set cursor out of the way */
  36.  
  37.   Example1();
  38.   Example2();
  39.   Example3();
  40.   Example4();
  41.   Example5();
  42.   Caption.Cursor = NRM_CSR;                  /* make cursor normal */
  43.   WWinCurSet(&Caption,Caption.UseLength,1);  /* restore cursor */
  44.   }/* end main */
  45.  
  46.  
  47.  
  48. void Example1()
  49.   {
  50.   /* this example shows how the WWinPut and WWinErase functions
  51.    * can be used to move a window around on the screen.
  52.    * note: the TopRow and LeftCol items in the WWinstruc can be changed
  53.    * at any time after the window has been defined. BUT- be sure the
  54.    * window is inactive (i.e., not on the screen) while changing these
  55.    * values.
  56.    * Also, you must make sure the entire window will fit on the screen
  57.    * to avoid a fatal error.
  58.   */
  59.   struct WWinstruc WinOne =
  60.     {"WinOne",5,3,8,10, BLUE,CYAN,SLN_BDR,RED,BLACK,INV_CSR};
  61.   int rc;
  62.   char inkey;
  63.   WWinLoad(&Caption,1,2,NC,NC,"EX-1 - Press arrow keys to move window",0);
  64.   WWinPut(&Caption);
  65.   if ((rc = WWinDef(&WinOne)) != 0)
  66.     {printf("out of memory"); exit(1);}
  67.   inkey = 0;
  68.   WWinPut(&WinOne);
  69.   while (inkey != 27)
  70.     {
  71.     WWinPut(&WinOne);
  72.     do                                                /* accept */
  73.       inkey = getch();                                /* only */
  74.     while ( (inkey != 0) && (inkey != 27) );          /* ctl keys and ESC */
  75.     if (inkey != 27)
  76.       {
  77.       inkey = getch();         /* get scan code */
  78.       switch(inkey)
  79.     {
  80.     case 72:      /* up arrow */
  81.       WWinErase(&WinOne);
  82.       if  (WinOne.TopRow > 1+(WinOne.Border > 0)) WinOne.TopRow--;
  83.       WWinPut(&WinOne);
  84.       break;
  85.     case 80:      /* down arrow */
  86.       WWinErase(&WinOne);
  87.       if  (WinOne.TopRow + WinOne.UseLength-(WinOne.Border == 0)
  88.          < WWScreenLength) WinOne.TopRow++;
  89.       WWinPut(&WinOne);
  90.       break;
  91.     case 75:      /* left arrow */
  92.       WWinErase(&WinOne);
  93.       if (WinOne.LeftCol > 1+(WinOne.Border > 0)) WinOne.LeftCol--;
  94.       WWinPut(&WinOne);
  95.       break;
  96.     case 77:      /* right arrow */
  97.       WWinErase(&WinOne);
  98.       if  (WinOne.LeftCol + WinOne.UseWidth-(WinOne.Border == 0)
  99.          < WWScreenWidth) WinOne.LeftCol++;
  100.       WWinPut(&WinOne);
  101.       break;
  102.     default:
  103.       break;
  104.     }
  105.       }/* if != 27 */
  106.     }/* while != 27 */
  107.   WWinDrop(&WinOne);        /* erase if necessary and free all memory */
  108.   }/* end Example1 */
  109.  
  110.  
  111.  
  112. void Example2()
  113.   {
  114.   /* this example demonstrates the WWinLoad and WWinClear functions
  115.    * including writing to the border
  116.   */
  117.   struct WWinstruc WinOne =
  118.     {"WinOne",5,10,4,70, BLUE,CYAN,DLN_BDR,RED,BLACK,INV_CSR};
  119.   int rc;
  120.   char inkey;
  121.   WWinLoad(&Caption,1,2,NC,NC,"EX-2 - Press ENTER to continue . . .     ",0);
  122.   WWinLoad(&Caption,1,15,NCB,NC,NULL,5);         /* blink the word ENTER */
  123.   WWinPut(&Caption);
  124.   if ((rc = WWinDef(&WinOne)) != 0)
  125.     {printf("out of memory"); exit(1);}
  126.   inkey = 0;
  127.   WWinLoad(&WinOne,WinOne.UseLength+1,30,NC,NC, /* load border with text */
  128.        "Example 2",0);
  129.   WWinLoad(&WinOne,1,2,NC,NC,
  130.       "This phrase is loaded with default background and text colors",0);
  131.   WWinPut(&WinOne);
  132.   inkey = getch();
  133.   if (inkey != 27)
  134.     {
  135.     WWinLoad(&WinOne,2,2,BLACK,RED,
  136.           "This phrase is loaded with black background and red text",0);
  137.     WWinPut(&WinOne);
  138.     inkey = getch();
  139.     if (inkey != 27)
  140.       {
  141.       WWinLoad(&WinOne,3,2,NCB,NCB,
  142.            "This phrase is loaded with blink default background"
  143.            " and bright         default text colors",0);
  144.       WWinPut(&WinOne);
  145.       inkey = getch();
  146.       if (inkey != 27)
  147.     {
  148.     WWinLoad(&WinOne,5,2,BLACK|BLINK,RED|BRIGHT,
  149.                "This phrase is loaded with blink black background"
  150.                " and bright red text",0);
  151.      WWinPut(&WinOne);
  152.     inkey = getch();
  153.     if (inkey != 27)
  154.       {
  155.       WWinLoad(&WinOne,7,2,NC,NC,
  156.                     "Half the previous phrase is changed"
  157.                     " to NOT BLINK and NOT BRIGHT",0);
  158.       WWinLoad(&WinOne,5,2,NCN,NCN,NULL,35);
  159.       WWinPut(&WinOne);
  160.       inkey = getch();
  161.       if (inkey != 27)
  162.         {
  163.           /* clears memory to blanks and attributes from structure */
  164.         WWinClear(&WinOne);
  165.         WWinPut(&WinOne);    /* puts cleared memory to screen */
  166.         inkey = getch();
  167.         }
  168.       }
  169.     }
  170.       }
  171.     }
  172.   WWinDrop(&WinOne);        /* erase if necessary and free all memory */
  173.   WWinLoad(&Caption,1,15,NCN,NC,NULL,5);         /* un-blink ENTER */
  174.   }/* end example 2 */
  175.  
  176.  
  177.  
  178. void Example3()
  179.   {
  180.   /* This example demonstrates changing the size and shape of a window
  181.    * and changing border values
  182.    * note: the size of a window can be changed after it is defined, BUT-
  183.    *   (1) the area of the window (length * width plus borders) must be set
  184.    *       to the maximum when WWinDef is executed.
  185.    *   (2) the window must be inactive when size changes are being made.
  186.    *   (3) if the window has borders, the WWinBorder function must be
  187.    *       executed after a size change to reset the borders.
  188.    *   (4) if the UseWidth is changed, the MemWidth value may be
  189.    *       identically changed to force wrap. If MemWidth is not
  190.    *       changed truncation occurs(more about this in the next
  191.    *       example).
  192.    * Also, you must make sure the entire window will fit on the screen
  193.    * to avoid a fatal error.
  194.   */
  195.   struct WWinstruc WinOne =
  196.     {"WinOne",5,15,10,30, BLUE,CYAN,DLN_BDR,RED,BLACK,INV_CSR};
  197.   int i, j, rc, maxsize;
  198.   char inkey, alpharow[3], alphacol[3];
  199.   extern unsigned char WWBorder_Cons[4][6];  /* access WW border styles */
  200.   unsigned char newborder[6] = {2,1,2,1,2,2};  /* smiley-face border */
  201.  
  202.   /* replace blank border with smiley faces */
  203.   memcpy(WWBorder_Cons[3],newborder,6);
  204.  
  205.   WWinLoad(&Caption,1,2,NC,NC,
  206.        "EX-3 - Press 'l' or 's' to re-size window & change borders",0);
  207.   WWinPut(&Caption);
  208.   if ((rc = WWinDef(&WinOne)) != 0)
  209.     {printf("out of memory"); exit(1);}
  210.   for(i=1;i<16;i++)                 /* load pattern into window-memory */
  211.     for(j=1;j<30;j+=15)
  212.       {
  213.       WWinLoad(&WinOne,i,j+1,NC,NC,"row ",0);
  214.       alpharow[0] = '0';
  215.       itoa(i,alpharow+(i<10),10);
  216.       WWinLoad(&WinOne,i,j+5,NC,NC,alpharow,0);
  217.       WWinLoad(&WinOne,i,j+7,NC,NC," col ",0);
  218.       itoa(j+14,alphacol,10);
  219.       WWinLoad(&WinOne,i,j+12,NC,NC,alphacol,0);
  220.       }
  221.   inkey = 0;
  222.      /* save max window area for test limit */
  223.   maxsize = WinOne.UseLength * WinOne.UseWidth;
  224.   while (inkey != 27)
  225.     {
  226.     WWinPut(&WinOne);
  227.     do
  228.       inkey = getch();
  229.     while ( (toupper(inkey) != 'S')
  230.      && (toupper(inkey) != 'L')
  231.      && (inkey != 27) ); /* only s & l keys and ESC */
  232.     if (inkey != 27)
  233.       {
  234.       WWinErase(&WinOne);
  235.       switch(toupper(inkey))
  236.     {
  237.     case 'S':      /* smaller */
  238.       if (WinOne.UseLength > 1) WinOne.UseLength--;
  239.       if (WinOne.UseWidth > 2)
  240.         {
  241.         WinOne.UseWidth-=2;
  242.         WinOne.MemWidth-=2;    /* force wrap */
  243.         }
  244.       WinOne.Border = (++WinOne.Border)%4;       /* cycle borders */
  245.       WWinBorder(&WinOne);                         /* for variety */
  246.       break;
  247.     case 'L':      /* larger */
  248.       if (WinOne.UseLength * WinOne.UseWid